<?php

namespace app\airobot\controller;

// 作者：卿鹏
use app\airobot\CommonLogic;
use think\admin\Controller;
use think\Exception;
use think\facade\Db;
use think\Request;

/**
 * 超管系统-Lucky
 * Class Lucky
 * @package app\airobot\controller
 */
class Lucky extends Controller
{
    public $commonLogic;

    /**
     * Lucky
     * @auth true
     */
    public function index()
    {
        $this->commonLogic = new CommonLogic();
        $userInfo = $this->app->session->get("user");
        $now = time();
        $today = date('Y-m-d') . " 00:00:00";

        // -------------- 头部汇总 -----------------
        try {
            // 总用户数量
            $sql = "select count(1) as ct from `user`";
            $users = Db::query($sql);
            if (count($users) == 0) {
                $$this->allUserCount = 0;
            } else {
                $this->allUserCount = $users[0]['ct'];
            }

            // 今日活跃数量
            $sql = "select count(1) as ct from `buried_point` where user_id != 0 and created_at > '" . $today . "' group by user_id";
            $todayAliveUsers = Db::query($sql);
            if (count($todayAliveUsers) == 0) {
                $this->todayAliveUsersCount = 0;
            } else {
                $this->todayAliveUsersCount = $todayAliveUsers[0]['ct'];
            }

            // 用户当前总资产
            $sql = "select sum(balance + git_balance) as totalBalance  from `user_balance`";
            $totalBalance = Db::query($sql);
            if (count($totalBalance) == 0) {
                $this->totalBalance = 0;
            } else {
                $this->totalBalance = $this->commonLogic->DealAmount($totalBalance[0]['totalBalance']);
            }

            // 用户充值总资产
            $sql = "select sum(usd_amount) as totalRechargeAmount  from `recharge_record` where status =1";
            $totalRechargeAmount = Db::query($sql);
            if (count($totalRechargeAmount) == 0) {
                $this->totalRechargeAmount = 0;
            } else {
                $this->totalRechargeAmount = $this->commonLogic->DealAmount($totalRechargeAmount[0]['totalRechargeAmount']);
            }

            // 用户总Spin资产
            $sql = "select sum(spin_amount) as totalUserSpinAmount  from `user_spin`";
            $totalUserSpinAmount = Db::query($sql);
            if (count($totalUserSpinAmount) == 0) {
                $this->totalUserSpinAmount = 0;
            } else {
                $this->totalUserSpinAmount = $this->commonLogic->DealAmount($totalUserSpinAmount[0]['totalUserSpinAmount']);
            }

            // 今日Spin资产
            $sql = "select sum(spin_amount) as totdayUserSpinAmount  from `user_spin` where created_at > '" . $today . "'";
            $totdayUserSpinAmount = Db::query($sql);
            if (count($totdayUserSpinAmount) == 0) {
                $this->totdayUserSpinAmount = 0;
            } else {
                $this->totdayUserSpinAmount = $this->commonLogic->DealAmount($totdayUserSpinAmount[0]['totdayUserSpinAmount']);
            }

            // 平台总盈利
            $sql = "select sum(spin_amount- product_amount) as systemTotalProfitAmount  from `user_spin`";
            $systemTotalProfitAmount = Db::query($sql);
            if (count($systemTotalProfitAmount) == 0) {
                $this->systemTotalProfitAmount = 0;
            } else {
                $this->systemTotalProfitAmount = $this->commonLogic->DealAmount($systemTotalProfitAmount[0]['systemTotalProfitAmount']);
            }

            // 平台今日盈利
            $sql = "select sum(spin_amount - product_amount) as systemTodayProfitAmount  from `user_spin`  where created_at > '" . $today . "'";
            $systemTodayProfitAmount = Db::query($sql);
            if (count($systemTodayProfitAmount) == 0) {
                $this->systemTodayProfitAmount = 0;
            } else {
                $this->systemTodayProfitAmount = $this->commonLogic->DealAmount($systemTodayProfitAmount[0]['systemTodayProfitAmount']);
            }

            // -------------- 今日概况 -----------------
            // 今日新增用户
            $yesterday = date('Y-m-d', strtotime('-1 day 00:00:00', $now)) . " 00:00:00";
            $sql = "select id from user where created_at > '" . $today . "'";
            $todayAddUsers = Db::query($sql);
            $this->todayAddUserCount = count($todayAddUsers);
            // 昨日新增用户
            $sql = "select id from user where created_at > '" . $yesterday . "' and created_at < '" . $today . "'";
            $yesterdayAddUsers = Db::query($sql);
            $this->yesterdayAddUserCount = count($yesterdayAddUsers);

            // 平台昨日盈利
            $sql = "select sum(spin_amount - product_amount) as systemYesterdayProfitAmount  from `user_spin`  where created_at > '" . $yesterday . "' and created_at < '" . $today . "'";
            $systemYesterdayProfitAmount = Db::query($sql);
            if (count($systemYesterdayProfitAmount) == 0) {
                $this->systemYesterdayProfitAmount = 0;
            } else {
                $this->systemYesterdayProfitAmount = $this->commonLogic->DealAmount($systemYesterdayProfitAmount[0]['systemYesterdayProfitAmount']);
            }

            // 充值资金
            // 今日
            $sql = "select sum(usd_amount) as todayRechargeAmount  from `recharge_record` where status =1 and created_at > '" . $today . "'";
            $todayRechargeAmount = Db::query($sql);
            if (count($todayRechargeAmount) == 0) {
                $this->todayRechargeAmount = 0;
            } else {
                $this->todayRechargeAmount = $this->commonLogic->DealAmount($todayRechargeAmount[0]['todayRechargeAmount']);
            }
            // 昨日
            $sql = "select sum(usd_amount) as yesterdayRechargeAmount  from `recharge_record` where status =1 and created_at > '" . $yesterday . "' and created_at < '" . $today . "'";
            $yesterdayRechargeAmount = Db::query($sql);
            if (count($yesterdayRechargeAmount) == 0) {
                $this->yesterdayRechargeAmount = 0;
            } else {
                $this->yesterdayRechargeAmount = $this->commonLogic->DealAmount($yesterdayRechargeAmount[0]['yesterdayRechargeAmount']);
            }

            // 提现资金
            // 总的
            $sql = "select sum(withdraw_amount) as totalWithdrawAmount  from `withdraw` where status =3 and created_at > '" . $today . "'";
            $totalWithdrawAmount = Db::query($sql);
            if (count($totalWithdrawAmount) == 0) {
                $this->totalWithdrawAmount = 0;
            } else {
                $this->totalWithdrawAmount = $this->commonLogic->DealAmount($totalWithdrawAmount[0]['totalWithdrawAmount']);
            }
            // 今日
            $sql = "select sum(withdraw_amount) as todayWithdrawAmount  from `withdraw` where status =3 and created_at > '" . $today . "'";
            $todayWithdrawAmount = Db::query($sql);
            if (count($todayWithdrawAmount) == 0) {
                $this->todayWithdrawAmount = 0;
            } else {
                $this->todayWithdrawAmount = $this->commonLogic->DealAmount($todayWithdrawAmount[0]['todayWithdrawAmount']);
            }
            // 昨日
            $sql = "select sum(withdraw_amount) as yesterdayWithdrawAmount  from `withdraw` where status =3 and created_at > '" . $yesterday . "' and created_at < '" . $today . "'";
            $yesterdayWithdrawAmount = Db::query($sql);
            if (count($yesterdayWithdrawAmount) == 0) {
                $this->yesterdayWithdrawAmount = 0;
            } else {
                $this->yesterdayWithdrawAmount = $this->commonLogic->DealAmount($yesterdayWithdrawAmount[0]['yesterdayWithdrawAmount']);
            }
        } catch (Exception $ex) {
            $this->error($ex->getMessage());
        }

        $this->manangeId = $userInfo["id"];
        $this->title = '首页';
        $this->fetch('index');
    }
}
